<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
	var tables = $('#dataTables').DataTable({
		"sAjaxSource" : "{{ urlfor "ArchivesController.Guahao"}}"
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
			var params = checkQueryTableData(aoData)
            $('#search_key').val() ? params.filter = $('#search_key').val() : '';
            params.startdate = $('#startdate').val() ? $('#startdate').val() : '';
            params.enddate = $('#enddate').val() ?$('#enddate').val() : '';
			$.post(sSource, JSON.stringify(params),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
            /*{"mDataProp" : "Number", "bSortable": false}*/
            {render:function(data, type, full, meta) {
                  return full.archives.Realname;
             }}
            ,{render:function(data, type, full, meta) {
                    return full.archives.Mobile;
            }}
            ,{render:function(data, type, full, meta) {
                    return full.archives.Sex;
                }}
            ,{render:function(data, type, full, meta) {
                    return full.archives.Age;
                }}
            ,{render:function(data, type, full, meta) {
                    return full.archives.Birthday;
                }}
			,{"mDataProp": "created_at", render:function(data, type, full, meta) {
				return timetrans(data);	
			}}
            ,{"mDataProp": "created_by", render:function(data, type, full, meta) {
                    return data;
                }}
            ,{"mDataProp": "status", render:function(data, type, full, meta) {
                    switch (data) {
                        case 0:
                            return '<span>待接诊</span>';
                            break;
                        case 1:
                            return '<span class="am-text-secondary">已接诊</span>';
                            break;
                        case 2:
                            return '<span class="am-text-danger">已退号</span>';
                            break;
                    }
                    return '';
                }}
            ,{
				"mDataProp": "Action", 
				"bSortable": false, 
				"sWidth":"8%",
				"render": function ( data, type, full, meta ) {
					btns = '<div class="tpl-table-black-operation">';
					if(full.status != 2) {
                        btns += '&nbsp;<a href="javascript:;" class="am-btn am-btn-danger am-btn-xs" onclick="delGuahao('+ full.id +');return false;">退号</a>';
                    }
					btns += '</div>';
					return btns;  
				}  
			 } 
		]
		,"aLengthMenu":[20, 50, 100, 200]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : true
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
		  
	});
	function dialogReback() {
		$("#dataTables").dataTable().fnDraw(false);
	}
	$("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
		$("#dataTables").dataTable().fnDraw(false);
	});

    function receiveFormData(data) {
       $.post('{{ urlfor "ArchivesController.GuahaoSave" }}',{'archives_id':data.Id}, function (res) {
            if(res.code == 1) {
                layer.msg(res.msg);
                return false;
            }
            layer.closeAll();
            $("#searchsubmit").click();
       },'json').error(function(){
           layer.msg('出错了！请联系管理员');
       });
    }
	function actionGuahao(id) {
		layer.open({
        	type: 2
			, title: '挂号'
			, shadeClose: true
			, shade: 0.2
			, id: 'search_archives_dialog'
			, area: ['1000px', "540px"]
			, content: ['{{ urlfor "ArchivesController.SelectArchives"}}']
			, anim: 1 //动画类型
		});
		return false;
	}
	

	function delGuahao(id) {
		layer.confirm('确定要退号吗？', function(index){
			$.post('{{ urlfor "ArchivesController.GuahaoOut"}}', {id:id}, function(res){
				if(res.code == 1) {
					layer.msg(res.msg)
				} else {
					tables.ajax.reload();
				}
				layer.close(index);
			}, 'json');
		});
	}
</script>
